import { createSlice, PayloadAction } from '@reduxjs/toolkit';

export interface AnalyticsData {
  conversionRate: {
    timeline: Array<{ date: string; rate: number; total: number; converted: number }>;
    byStage: Array<{ stage: string; rate: number; count: number }>;
  };
  valueDistribution: Array<{ range: string; count: number; percentage: number }>;
  industryAnalysis: Array<{ industry: string; count: number; value: number; avgValue: number }>;
  funnelData: Array<{
    stage: string;
    count: number;
    rate: number;
    avgDuration: number;
  }>;
  teamPerformance: Array<{
    member: string;
    opportunities: number;
    converted: number;
    rate: number;
    totalValue: number;
    avgDealSize: number;
  }>;
  trends: {
    monthly: Array<{ month: string; new: number; closed: number; value: number }>;
    quarterly: Array<{ quarter: string; new: number; closed: number; value: number }>;
  };
}

export interface AnalyticsFilters {
  dateRange: [string, string];
  department: string[];
  members: string[];
  industry: string[];
  valueRange: [number, number];
}

export interface AnalyticsState {
  data: AnalyticsData;
  filters: AnalyticsFilters;
  loading: boolean;
  error: string | null;
  exportLoading: boolean;
}

const initialState: AnalyticsState = {
  data: {
    conversionRate: {
      timeline: [],
      byStage: [],
    },
    valueDistribution: [],
    industryAnalysis: [],
    funnelData: [],
    teamPerformance: [],
    trends: {
      monthly: [],
      quarterly: [],
    },
  },
  filters: {
    dateRange: ['', ''],
    department: [],
    members: [],
    industry: [],
    valueRange: [0, 10000000],
  },
  loading: false,
  error: null,
  exportLoading: false,
};

const analyticsSlice = createSlice({
  name: 'analytics',
  initialState,
  reducers: {
    setLoading: (state, action: PayloadAction<boolean>) => {
      state.loading = action.payload;
    },
    setExportLoading: (state, action: PayloadAction<boolean>) => {
      state.exportLoading = action.payload;
    },
    setError: (state, action: PayloadAction<string | null>) => {
      state.error = action.payload;
    },
    setData: (state, action: PayloadAction<AnalyticsData>) => {
      state.data = action.payload;
    },
    updateConversionRate: (state, action: PayloadAction<AnalyticsData['conversionRate']>) => {
      state.data.conversionRate = action.payload;
    },
    updateValueDistribution: (state, action: PayloadAction<AnalyticsData['valueDistribution']>) => {
      state.data.valueDistribution = action.payload;
    },
    updateIndustryAnalysis: (state, action: PayloadAction<AnalyticsData['industryAnalysis']>) => {
      state.data.industryAnalysis = action.payload;
    },
    updateFunnelData: (state, action: PayloadAction<AnalyticsData['funnelData']>) => {
      state.data.funnelData = action.payload;
    },
    updateTeamPerformance: (state, action: PayloadAction<AnalyticsData['teamPerformance']>) => {
      state.data.teamPerformance = action.payload;
    },
    updateTrends: (state, action: PayloadAction<AnalyticsData['trends']>) => {
      state.data.trends = action.payload;
    },
    setFilters: (state, action: PayloadAction<Partial<AnalyticsFilters>>) => {
      state.filters = { ...state.filters, ...action.payload };
    },
    resetFilters: (state) => {
      state.filters = {
        dateRange: ['', ''],
        department: [],
        members: [],
        industry: [],
        valueRange: [0, 10000000],
      };
    },
  },
});

export const {
  setLoading,
  setExportLoading,
  setError,
  setData,
  updateConversionRate,
  updateValueDistribution,
  updateIndustryAnalysis,
  updateFunnelData,
  updateTeamPerformance,
  updateTrends,
  setFilters,
  resetFilters,
} = analyticsSlice.actions;

export default analyticsSlice.reducer;
